package com.estore.managerImpl;

import java.util.ArrayList;
import java.util.List;

import com.estore.dao.User_Dao;
import com.estore.entity.Account;
import com.estore.entity.Role;
import com.estore.entity.User_Profile;
import com.estore.info.AccountInfo;
import com.estore.info.ProfileInfo;
import com.estore.info.RegisterInfo;
import com.estore.manager.ProfileManager;

public class ProfileManagerImpl implements ProfileManager {
	private User_Dao user_dao;

	// list all profile include account & role
	@Override
	public List<ProfileInfo> getListProfileInfo() {
		List<ProfileInfo> profileInfo = new ArrayList<ProfileInfo>();
		List<User_Profile> listProfileInfo = user_dao.listUser_Profile();
		for (User_Profile record : listProfileInfo) {
			Account account = user_dao.getAccount(record.getUserID());
			Role role = user_dao.getRole(record.getUserID());
			profileInfo.add(new ProfileInfo(record.getUserID(), record
					.getLastName(), record.getFirstName(), record.getEmail(),
					record.getDateOfBirth(), record.getSex(), record
							.getAddress(), record.getPhoneNumber(), account
							.getAccount(), role.getRole(), role.getIsBanned()));
		}
		return profileInfo;
	}

	// check username / password, return username / userID
	@Override
	public AccountInfo getAccountInfo(String user, String pass) {
		if (user_dao.getAccount(user, pass) != null) {
			Account profile = user_dao.getAccount(user, pass);
			AccountInfo info = new AccountInfo();
			info.setAccount(profile.getAccount());
			info.setUserID(profile.getUserID());

			return info;
		} else
			return null;
	}

	public User_Dao getUser_dao() {
		return user_dao;
	}

	public void setUser_dao(User_Dao user_dao) {
		this.user_dao = user_dao;
	}

	@Override
	public void editProfileInfo(int userID, String lastName, String firstName,
			String email, String dateOfBirth, String sex, String address,
			String phoneNumber) {
		user_dao.updateProfile(userID, lastName, firstName, email, dateOfBirth,
				sex, address, phoneNumber);
	}

	@Override
	public ProfileInfo getProfileInfo(int userID) {
		if (user_dao.getUser_Profile(userID) != null) {
			User_Profile profile = user_dao.getUser_Profile(userID);
			ProfileInfo info = new ProfileInfo();

			if (profile != null) {
				info.setUserID(profile.getUserID());
				info.setFirstName(profile.getFirstName());
				info.setLastName(profile.getLastName());
				info.setEmail(profile.getEmail());
				info.setSex(profile.getSex());
				info.setDateOfBirth(profile.getDateOfBirth());
				info.setAddress(profile.getAddress());
				info.setPhoneNumber(profile.getPhoneNumber());
			}
			return info;
		} else
			return null;
	}

	@Override
	public Role getRole(int userID) {
		Role role = user_dao.getRole(userID);
		return role;
	}

	@Override
	public Account getAccountInfo(int userID) {
		Account account = user_dao.getAccount(userID);
		return account;
	}

	@Override
	public void register(RegisterInfo registerInfo) {
		Account account = new Account(registerInfo.getAccount(),
				registerInfo.getPassword(), registerInfo.getSecretQuestion(),
				registerInfo.getSecretAnswer());
		User_Profile profile = new User_Profile(registerInfo.getLastName(),
				registerInfo.getFirstName(), registerInfo.getEmail(),
				registerInfo.getDateOfBirth(), registerInfo.getSex(),
				registerInfo.getAddress(), registerInfo.getPhoneNumber(), registerInfo.getRegDate());
		Role role = new Role(registerInfo.getRole());
		user_dao.insertUser_Profile(profile);
		user_dao.insertAccount(account);
		user_dao.insertRole(role);
	}

	@Override
	public boolean checkAccountAvail(String account) {
		if (user_dao.checkAccountAvail(account) == true) {
			return true;
		} else
			return false;
	}

	@Override
	public boolean checkEmailAvail(String email) {
		if (user_dao.checkEmailAvail(email) == true) {
			return true;
		}
		return false;
	}

	@Override
	public void updateRole(Integer userID) {
		user_dao.updateRole(userID);
	}
}
